﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ResUma
{
    public partial class CertificateMedicaleFrm : FrmCard
    {
        private Int16 AnulCurent = 0;
        private Int16 LunaCurenta = 0;
        DateTime ultimazi = DateTime.MinValue;

        public CertificateMedicaleFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
            workConnString = "ESRPConnectionString";
            ///eSRPDataSet.ConcediiMedicale.ZileBoalaCumulateColumn.DefaultValue = 0;
            //eSRPDataSet.ConcediiMedicale.ZileCASCumulateColumn.DefaultValue = 0;
            ///eSRPDataSet.ConcediiMedicale.ZileFAMBPCumulateColumn.DefaultValue = 0;
        }

  
        private void CertificateMedicaleFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(concediiMedicaleBindingSource);
            listTableAdaptor.Add(concediiMedicaleTableAdapter.Adapter);

            SqlConnection con = DeschideConexiune(workConnString);
            Setup(con);
            ultimazi = new DateTime(AnulCurent, LunaCurenta, 1).AddMonths(1).AddDays(-1);
            this.tipBoalaTableAdapter.Connection = con;
            this.tipBoalaTableAdapter.Fill(this.eSRPDataSet.TipBoala);

            this.resuma_GenereazaListaPersonalAngajatTableAdapter.Connection = con;
            this.resuma_GenereazaListaPersonalAngajatTableAdapter.Fill(this.eSRPDataSet.resuma_GenereazaListaPersonalAngajat, ultimazi, ModuleInfo.CodUnitate);
            this.concediiMedicaleTableAdapter.Connection = con;
            this.concediiMedicaleTableAdapter.Fill(this.eSRPDataSet.ConcediiMedicale);

            InchideConexiune(con);
            HandleSalvare += Salvare;
            if (concediiMedicaleBindingSource.Count == 0)
                Adauga(this, null);

        }

        private void Setup(SqlConnection sqlConn)
        {
            Object ret = GetSetupValue("ResUmaSetup", "AnulCurent", ModuleInfo.CodUnitate, sqlConn);
            /// nu a fost configurat modulul
            /// 
            while (ret == null)
            {
                MessageBox.Show("Modulul Resurse Umane nu a fost configurat. \nVa rugam sa-l configurati!",
                        "Eroare",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);
                ResUmaSetupFrm frm = new ResUmaSetupFrm(ModuleInfo);
                frm.ShowDialog();
                ret = GetSetupValue("ResUmaSetup", "AnulCurent", ModuleInfo.CodUnitate, sqlConn);
            }
            AnulCurent = (Int16)ret;
            ret = GetSetupValue("ResUmaSetup", "LunaCurenta", ModuleInfo.CodUnitate, sqlConn);
            LunaCurenta = (Int16)ret;
            SetInfoLunaAnul(AnulCurent.ToString() +"," + LunaCurenta.ToString());
        }

        private void idDepartamentComboBox_Enter(object sender, EventArgs e)
        {

        }

        private void idPersoanaComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (idPersoanaCB.SelectedItem != null)
            {
                ESRPDataSet.resuma_GenereazaListaPersonalAngajatRow par = ((DataRowView)idPersoanaCB.SelectedItem).Row as
                    ESRPDataSet.resuma_GenereazaListaPersonalAngajatRow;
                SqlConnection con = DeschideConexiune(workConnString);
                this.resuma_GenereazaListaDepartamentePATableAdapter.Connection = con;
                this.resuma_GenereazaListaDepartamentePATableAdapter.Fill(this.eSRPDataSet.resuma_GenereazaListaDepartamentePA,par.IdPersoana, ultimazi, ModuleInfo.CodUnitate);
                InchideConexiune(con);
                resumaGenereazaListaDepartamentePABindingSource.ResetCurrentItem();

                resumaGenereazaListaDepartamentePABindingSource.ResetBindings(false);
                idDepartamentCB.Refresh();
            }
        }
    }
}
